Shader "ShaderMan/MyShader"
{

    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
    }

    SubShader
    {
        Tags
        {
            "RenderType" = "Transparent" "Queue" = "Transparent"
        }

        Pass
        {
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct VertexInput
            {
                fixed4 vertex : POSITION;
                fixed2 uv:TEXCOORD0;
                fixed4 tangent : TANGENT;
                fixed3 normal : NORMAL;
                //VertexInput
            };


            struct VertexOutput
            {
                fixed4 pos : SV_POSITION;
                fixed2 uv:TEXCOORD0;
                //VertexOutput
            };

            //Variables

            #define SCALE 10.0
            #define STEP 4.0
            #define GRASSPIECE 0.95
            #define UPP 0.125
            #define SPEED 0.4
            #define STR 0.2

            #define FLOWER 0.995

            fixed hash2to1(fixed2 p)
            {
                fixed3 p3 = frac(fixed3(p.x, p.y, p.x) * .1031);
                p3 += dot(p3, p3.yzx + 33.33);
                return frac((p3.x + p3.y) * p3.z);
            }

            fixed hash3to1(fixed3 p3)
            {
                p3 = frac(p3 * .1031);
                p3 += dot(p3, p3.zyx + 31.32);
                return frac((p3.x + p3.y) * p3.z);
            }

            fixed FlowerCenter(fixed2 uv)
            {
                fixed2 pixUV = floor(uv / UPP) * UPP;
                return ceil(hash2to1(pixUV) - FLOWER);
            }

            fixed FlowerNeighbor(fixed2 uv)
            {
                fixed2 pixUV = floor(uv / UPP) * UPP;
                fixed2 offset = fixed2(0, UPP);
                return max(FlowerCenter(uv + offset.xy), max(FlowerCenter(uv - offset.xy),
                               max(FlowerCenter(uv + offset.yx), FlowerCenter(uv - offset.yx))));
            }


            fixed FlowerShadow(fixed2 uv)
            {
                fixed2 pixUV = floor(uv / UPP) * UPP;
                fixed3 offset = fixed3(-UPP, UPP, 0);
                return max(FlowerCenter(uv + offset.zy * 2.0),
    max(FlowerCenter(uv + offset.yy), FlowerCenter(uv + offset.xy)));
            }


            VertexOutput vert(VertexInput v)
            {
                VertexOutput o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                //VertexFactory
                return o;
            }

            fixed4 frag(VertexOutput i) : SV_Target
            {
                // Normalized pixel coordinates (from 0 to 1)
                fixed2 uv = i.uv / 1;

                uv = uv * SCALE;

                // uv.y += _Time.y * 1.0;//取消位移

                fixed2 subuv = frac(uv);
                fixed2 coord = floor(uv);
                fixed noiseForTime = hash2to1(coord) + floor((subuv.x - 0.5) / UPP) * UPP;

                fixed sinTime = floor(sin(_Time.y * SPEED + noiseForTime) * STR / UPP) * UPP;
                fixed subuvx = floor((subuv.x + sinTime) / UPP) * UPP;
                fixed noiseForBottom = hash3to1(fixed3(coord, subuvx));

                fixed isBottomPixel = 1.0 - max(ceil(noiseForBottom - 0.3), step(UPP, subuv.y));

                coord.y -= isBottomPixel;

                fixed2 grassPieceUV = fixed2(uv.x + sinTime, uv.y * 0.5);
                fixed noiseForGrassPiece = ceil(hash2to1(floor(grassPieceUV / UPP) * UPP) - GRASSPIECE);
                noiseForGrassPiece = noiseForGrassPiece * (noiseForGrassPiece * 2.0 - 1.0) * 0.25;

                fixed noise = hash2to1(coord) + noiseForGrassPiece;

                fixed gray = ceil(noise * STEP) / STEP;

                fixed3 c1 = fixed3(0.3, 0.5, 0.3);
                fixed3 c2 = fixed3(0.6, 0.7, 0.2);

                fixed3 col = lerp(c1, c2, gray);

                fixed flowerNeighbor = FlowerNeighbor(uv);
                col = lerp(col, fixed3(1.0, 0.95, 0.9), flowerNeighbor);

                fixed flower = FlowerCenter(uv);
                col = lerp(col, fixed3(1.0, 0.9, 0.0), flower);

                fixed flowerShadow = FlowerShadow(uv);
                col = lerp(col, col - 0.15, flowerShadow);

                // Output to screen
                return fixed4(col, 1.0);
            }
            ENDCG
        }
    }
}